package org.metasketch.diagram.javafx.graphics;

import java.util.ArrayList;
import java.util.List;

import org.metasketch.editor.links.LineSection;
import org.metasketch.graphics.GraphicLink;
import org.metasketch.graphics.Point;

import javafx.scene.Group;
import javafx.scene.shape.Line;

public class SelectLine extends Group {

	BaseLink baseLink;
	boolean selected = false;

	public SelectLine(BaseLink baseLink) {
		this.baseLink = baseLink;

		setTranslateX(0);
		setTranslateY(0);
	}

	public void setSelected(boolean selected) {
		if (this.selected == selected)
			return;

		this.selected = selected;

		if (!selected) {
			getChildren().clear();
			baseLink.getChildren().remove(this);
			return;
		}

		GraphicLink graphicLink = (GraphicLink) baseLink.getGraphicNode();
		List<Point> points = graphicLink.getPoints();

		List<LineSection> lines = baseLink.getLines();

		List<LineControlPoint> controlPoints = new ArrayList<LineControlPoint>();

		for (int i = 0; i < points.size(); i++) {

			Point point = points.get(i);
			LineSection previous = null;
			LineSection next = null;

			if (i - 1 >= 0)
				previous = lines.get(i - 1);

			if (i < lines.size())
				next = lines.get(i);

			LineControlPoint controlPoint = new LineControlPoint(baseLink);
			controlPoint.setLines(previous, next, point);
			controlPoints.add(controlPoint);
		}

		getChildren().addAll(controlPoints);

		baseLink.getChildren().add(this);

	}

	public boolean isSelected() {
		return selected;
	}

}
